# $Id$
# Maintainer: J. Peter Mugaas <jpmugaas@suddenlink.net>
#stolen from Archlinux with some modifications for our layout
_realname=sgml-common
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.6.3
pkgrel=1
pkgdesc='Tools for maintaining centralized SGML catalogs. (mingw-w64)'
arch=('any')
url='https://www.docbook.org/xml/'
license=('GPL')
install=sgml-common-${CARCH}.install
depends=('sh')
source=("https://www.sourceware.org/pub/docbook-tools/new-trials/SOURCES/sgml-common-${pkgver}.tgz"
        'sgml-common-0.6.3-manpage-1.patch'
        'sgml-common-0.6.3-sysconfdir.patch')
noextract=("sgml-common-${pkgver}.tgz")
sha512sums=('3c4a55f555596b2a6bf6af66c497679226e7b40625ac16832150488311cba7f9fc523435eea5837262a5517f5c09c4c0e07fe4a2cf02184d4027609c26e4bc0c'
            'c584ff6e66e1d58456c07849bf96fedd3f1df0b93430f6301c4d4ab9906821dea0cbac82eada19197540b5e31f13febd04fadda5b6e2ba4b0b6ee8f0328292f7'
            '3385de6890d7370a8fa575e13aead270bd2b19687f40305867c3b208d671d3582f063109b11b0b39783a3b0e010f48e49e739cd44d11c1d5245fa6046271492a')

prepare() {
  cd $srcdir
  # work around an issue with symbolic links in the tarball.
  /usr/bin/bsdtar zxvf $srcdir/sgml-common-${pkgver}.tgz || true 
  cd "${srcdir}"/${_realname}-${pkgver}
  # The symbolic links themselves were simply linking files from /usr/share/automake to the dist folder.
  cp /usr/share/automake-1.15/install-sh .
  cp /usr/share/automake-1.15/mkinstalldirs .
  cp /usr/share/automake-1.15/missing .
  cp /usr/share/automake-1.15/INSTALL .
  cp /usr/share/automake-1.15/COPYING .
  patch -Np1 -i $srcdir/sgml-common-0.6.3-manpage-1.patch
  patch -Np1 -i $srcdir/sgml-common-0.6.3-sysconfdir.patch
  autoreconf -vfi
}

build() {
  cd "$srcdir"/${_realname}-${pkgver}
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} \
   && cp -Rf * "${srcdir}"/build-${CARCH} \
   && cd "${srcdir}"/build-${CARCH}
   ./configure --prefix=${MINGW_PREFIX} \
    --sysconfdir=${MINGW_PREFIX}/etc \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST}
  make
}
package(){
  cd "${srcdir}"/build-${CARCH}
  make DESTDIR="$pkgdir" docdir=${MINGW_PREFIX}/share/doc install
# patch sgmlwhich so it will work with our setup
  sed -i -e "s|\/etc\/|${MINGW_PREFIX}\/etc\/|g" $pkgdir${MINGW_PREFIX}/bin/sgmlwhich

#setup our configuration for sgml/sgml/conf
  sed -i -e "s|SGML_BASE_DIR=\/usr\/share\/sgml|SGML_BASE_DIR=${MINGW_PREFIX}/share/sgml|g" \
         -e "s|SGML_CATALOGS_DIR=\/etc\/sgml|SGML_CATALOGS_DIR=${MINGW_PREFIX}\/etc\/sgml|g" \
      $pkgdir${MINGW_PREFIX}/etc/sgml/sgml.conf
  
#   "$pkgdir/usr/doc" "$pkgdir/usr/share/doc"
}

